﻿using System;
using System.Text;
using System.Data;
using System.Data.OleDb;
using System.Collections.Generic;
using System.Reflection;
using System.IO;
using System.Net;

namespace BLL
{
    //Newscont
    public static class LotteryAnnouncement
    {
        private static Data.LotteryAnnouncement Data_Newscont = new Data.LotteryAnnouncement();

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public static long Add(Model.LotteryAnnouncement model)
        {
            return Data_Newscont.Add(model);
        }
        //wlq20151112
        public static List<Model.LotteryAnnouncement> GetListByTypeWeek(int pageIndex, int pageSize, int type, int WeekNumbmer, out int totalCount)
        {
            return Data_Newscont.GetListByTypeWeek(pageIndex, pageSize, type, WeekNumbmer, out totalCount);
        }
        public static List<Model.LotteryAnnouncement> GetListByTypeWeek1(int pageIndex, int pageSize, int type, int WeekNumbmer, out int totalCount)
        {
            return Data_Newscont.GetListByTypeWeek1(pageIndex, pageSize, type, WeekNumbmer, out totalCount);
        }
        public static List<Model.LotteryAnnouncement> GetListByShuangseDSqi(int pageIndex, int pageSize, int type, int dsQi, out int totalCount)
        {
            return Data_Newscont.GetListByShuangseDSqi(pageIndex, pageSize, type, dsQi, out totalCount);
        }
        public static List<Model.LotteryAnnouncement> GetListByShuangseDSqi1(int pageIndex, int pageSize, int type, int dsQi, out int totalCount)
        {
            return Data_Newscont.GetListByShuangseDSqi1(pageIndex, pageSize, type, dsQi, out totalCount);
        }
        //wlq20151112
        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public static int Update(Model.LotteryAnnouncement model)
        {
            return Data_Newscont.Update(model);
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public static bool Delete(long Id)
        {
            return Data_Newscont.Delete(Id);
        }
        /// <summary>
        /// 单条
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public static Model.LotteryAnnouncement GetModel(long Id)
        {
            return Data_Newscont.GetModel(Id);
        }
        public static Model.LotteryAnnouncement Prv(int type)
        {
            string sql = "select top 1 * from LotteryAnnouncement where LotteryType=" + type + "  order by LotteryQS desc";

            return Data_Newscont.GetModelBysql(sql);

        }
        public static Model.LotteryAnnouncement GetModelBySql(string sql)
        {
            return Data_Newscont.GetModelBysql(sql);

        }
        /// <summary>
        /// 下一篇
        /// </summary>
        /// <param name="IndexID"></param>
        /// <param name="tName"></param>
        /// <param name="condition"></param>
        /// <returns></returns>
        public static Model.LotteryAnnouncement getModelByLotteryType(int LotteryType)
        {
            string sql = "SELECT top 1 * FROM LotteryAnnouncement WHERE LotteryType=" + LotteryType + " ORDER BY Id desc";

            return Data_Newscont.GetModelBysql(sql);

        }
        /// <summary>
        /// 总数据数
        /// </summary>
        /// <returns></returns>
        public static int GetAll_Count()
        {
            return Data_Newscont.GetAll_Count();
        }
        /// <summary>
        /// 抽取分页数据
        /// </summary>
        public static List<Model.LotteryAnnouncement> GetPageList_Where(int pageIndex, int pageSize, long newsclassid, out int totalCount)
        {
            return Data_Newscont.GetPageList_Where(pageIndex, pageSize, newsclassid, out totalCount);
        }
        public static List<Model.LotteryAnnouncement> GetPageList_Where1(int pageIndex, int pageSize, long newsclassid, out int totalCount)
        {
            return Data_Newscont.GetPageList_Where1(pageIndex, pageSize, newsclassid, out totalCount);
        }
        public static List<Model.LotteryAnnouncement> GetPageList_Where2(int pageIndex, int pageSize, long newsclassid, out int totalCount)
        {
            return Data_Newscont.GetPageList_Where2(pageIndex, pageSize, newsclassid, out totalCount);
        }
        public static DataTable GetPageDT_Where(int PageIndex, int PageSize, long newsclassid, out int totalCount)
        {
            return Data_Newscont.GetPageDT_Where(PageIndex, PageSize, newsclassid, out totalCount);
        }
        public static List<Model.LotteryAnnouncement> GetPageListSQl(int PageIndex, int PageSize, string sql, out int totalCount)
        {
            return Data_Newscont.GetPageListSQl(PageIndex, PageSize, sql, out totalCount);
        }
        public static List<Model.LotteryAnnouncement> GetListByTypeQs(int pageIndex, int pageSize, int type, string qs, out int totalCount)
        {
            return Data_Newscont.GetListByTypeQs(pageIndex, pageSize, type, qs, out totalCount);
        }
        /// <summary>
        /// 批删
        /// </summary>
        /// <param name="Ids"></param>
        /// <returns></returns>
        public static int Drop(string Ids)
        {
            return Data_Newscont.Drop(Ids);
        }
        //----------------------------扩展方法----------------------------------------
        public static DataTable GetPageListBysql(string sql)
        {
            return Data_Newscont.GetPageListBysql(sql);
        }
        public static List<Model.LotteryAnnouncement> GetList(string Sql)
        {
            return Data_Newscont.GetList(Sql);
        }

        public static List<Model.LotteryAnnouncement> GetListOrderBy(string OrderBy, string WhereStr, string topStr, string ColumnNmae)
        {
            return Data_Newscont.GetListOrderBy(OrderBy, WhereStr, topStr, ColumnNmae);
        }

        public static int GetAll_Count(string whereStr)
        {
            return Data_Newscont.GetAll_Count(whereStr);
        }
        //public static List<Model.LotteryAnnouncement> GetAnnouncement(string LotteryType, string LotteryQS)
        //{
        //    string sql = "select LotteryNo,openTime from LotteryAnnouncement where LotteryType='" + LotteryType + "' and LotteryQS='" + LotteryQS + "'";
        //    List<Model.LotteryAnnouncement> list = BLL.LotteryAnnouncement.GetList(sql);
        //    return list;
        //}
        //============================================自动更新开奖公告==================================================

        public static long GetHtmlEx(string url, Encoding encoding, int type)
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
            request.ContentType = "application/x-www-form-urlencoded";
            request.Accept = "*";
            request.Method = "get";
            request.AllowAutoRedirect = false;
            request.Timeout = 50000;

            WebResponse response = request.GetResponse();
            Stream responseStream = response.GetResponseStream();
            StreamReader reader = new StreamReader(responseStream, encoding);
            String html = reader.ReadToEnd();//全部读出来

            long r = 0;
            string[] allLines;
            if (type == 1)
            {
                string filePath = System.Web.HttpContext.Current.Server.MapPath(@"\App_Data\ssq.txt");
                FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.ReadWrite);
                StreamWriter sw = new StreamWriter(fs);
                fs.SetLength(0);//把文件清空了。
                sw.Write(html);//写字符串。
                sw.Close();
                response.Close();
                allLines = File.ReadAllLines(System.Web.HttpContext.Current.Server.MapPath(@"\App_Data\ssq.txt"));
                string[] newdata = allLines[allLines.Length - 1].Split(' ');//最新一条数据开奖数据
                r = GetModel(newdata, 1);
            }
            else if (type == 2)
            {
                string filePath = System.Web.HttpContext.Current.Server.MapPath(@"\App_Data\dlt.txt");
                FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.ReadWrite);
                StreamWriter sw = new StreamWriter(fs);
                fs.SetLength(0);//把文件清空了。
                sw.Write(html);//写字符串。
                sw.Close();
                response.Close();
                allLines = File.ReadAllLines(System.Web.HttpContext.Current.Server.MapPath(@"\App_Data\dlt.txt"));
                string[] newdata = allLines[allLines.Length - 1].Split(' ');//最新一条数据开奖数据
                r = GetModel(newdata, 2);
            }
            else if (type == 3)
            {
                string filePath = System.Web.HttpContext.Current.Server.MapPath(@"\App_Data\p3.txt");
                FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.ReadWrite);
                StreamWriter sw = new StreamWriter(fs);
                fs.SetLength(0);//把文件清空了。
                sw.Write(html);//写字符串。
                sw.Close();
                response.Close();
                allLines = File.ReadAllLines(System.Web.HttpContext.Current.Server.MapPath(@"\App_Data\p3.txt"));
                string[] newdata = allLines[allLines.Length - 1].Split(' ');//最新一条数据开奖数据
                r = GetModel(newdata, 3);
            }
            else if (type == 4)
            {
                string filePath = System.Web.HttpContext.Current.Server.MapPath(@"\App_Data\p5.txt");
                FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.ReadWrite);
                StreamWriter sw = new StreamWriter(fs);
                fs.SetLength(0);//把文件清空了。
                sw.Write(html);//写字符串。
                sw.Close();
                response.Close();
                allLines = File.ReadAllLines(System.Web.HttpContext.Current.Server.MapPath(@"\App_Data\p5.txt"));
                string[] newdata = allLines[allLines.Length - 1].Split(' ');//最新一条数据开奖数据
                r = GetModel(newdata, 4);
            }
            else if (type == 5)
            {
                string filePath = System.Web.HttpContext.Current.Server.MapPath(@"\App_Data\3d.txt");
                FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.ReadWrite);
                StreamWriter sw = new StreamWriter(fs);
                fs.SetLength(0);//把文件清空了。
                sw.Write(html);//写字符串。
                sw.Close();
                response.Close();
                allLines = File.ReadAllLines(System.Web.HttpContext.Current.Server.MapPath(@"\App_Data\3d.txt"));
                string[] newdata = allLines[allLines.Length - 1].Split(' ');//最新一条数据开奖数据
                r = GetModel(newdata, 5);
            }
            else if (type == 6)
            {
                string filePath = System.Web.HttpContext.Current.Server.MapPath(@"\App_Data\7xc.txt");
                FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.ReadWrite);
                StreamWriter sw = new StreamWriter(fs);
                fs.SetLength(0);//把文件清空了。
                sw.Write(html);//写字符串。
                sw.Close();
                response.Close();
                allLines = File.ReadAllLines(System.Web.HttpContext.Current.Server.MapPath(@"\App_Data\7xc.txt"));
                string[] newdata = allLines[allLines.Length - 1].Split(' ');//最新一条数据开奖数据
                r = GetModel(newdata, 6);
            }
            else if (type == 7)
            {
                string filePath = System.Web.HttpContext.Current.Server.MapPath(@"\App_Data\7lc.txt");
                FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.ReadWrite);
                StreamWriter sw = new StreamWriter(fs);
                fs.SetLength(0);//把文件清空了。
                sw.Write(html);//写字符串。
                sw.Close();
                response.Close();
                allLines = File.ReadAllLines(System.Web.HttpContext.Current.Server.MapPath(@"\App_Data\7lc.txt"));
                string[] newdata = allLines[allLines.Length - 1].Split(' ');//最新一条数据开奖数据
                r = GetModel(newdata, 7);
            }


            return r;
        }

        private static long GetModel(string[] data, int type)
        {
            long r = 0;
            if (type == 1)
            {
                #region 双色球
                Model.LotteryAnnouncement model = new Model.LotteryAnnouncement();
                model.AddTime = System.DateTime.Now;
                model.LotteryType = 1;
                model.LotteryQS = data[0];
                model.openTime = DateTime.Parse(data[1]);
                model.LotteryNo = data[2] + "," + data[3] + "," + data[4] + "," + data[5] + "," + data[6] + "," + data[7] + "+" + data[8];
                model.AppearOrderNo = data[9] + "," + data[10] + "," + data[11] + "," + data[12] + "," + data[13] + "," + data[14];
                model.sumMoney = decimal.Parse(data[15]);
                model.jiangChi = decimal.Parse(data[16]);
                model.oneCount = int.Parse(data[17]);
                model.oneMoney = decimal.Parse(data[18]);
                model.twoCount = int.Parse(data[19]);
                model.twoMoney = decimal.Parse(data[20]);
                model.threeCount = int.Parse(data[21]);
                model.threeMoney = decimal.Parse(data[22]);
                model.fourCount = int.Parse(data[23]);
                model.fourMoney = decimal.Parse(data[24]);
                model.fiveCount = int.Parse(data[25]);
                model.fiveMoney = decimal.Parse(data[26]);
                model.sixCount = int.Parse(data[27]);
                model.sixMoney = decimal.Parse(data[28]);
                r = addWinInfo(model);
                #endregion
            }
            else if (type == 2)
            {
                #region 大乐透
                Model.LotteryAnnouncement model = new Model.LotteryAnnouncement();
                model.AddTime = System.DateTime.Now;
                model.LotteryType = 2;
                model.LotteryQS = data[0];
                model.openTime = DateTime.Parse(data[1]);
                model.LotteryNo = data[2] + "," + data[3] + "," + data[4] + "," + data[5] + "," + data[6] + "+" + data[7] + "," + data[8];
                model.AppearOrderNo = "0";
                model.sumMoney = decimal.Parse(data[9]);
                model.jiangChi = decimal.Parse(data[10]);
                model.oneCount = int.Parse(data[11]);
                model.oneMoney = decimal.Parse(data[12]);
                model.twoCount = int.Parse(data[13]);
                model.twoMoney = decimal.Parse(data[14]);
                model.threeCount = int.Parse(data[15]);
                model.threeMoney = decimal.Parse(data[16]);
                model.fourCount = int.Parse(data[17]);
                model.fourMoney = decimal.Parse(data[18]);
                model.fiveCount = int.Parse(data[19]);
                model.fiveMoney = decimal.Parse(data[20]);
                model.sixCount = int.Parse(data[21]);
                model.sixMoney = decimal.Parse(data[22]);
                r = addWinInfo(model);
                #endregion
            }
            else if (type == 3)
            {
                #region 排三
                Model.LotteryAnnouncement model = new Model.LotteryAnnouncement();
                model.AddTime = System.DateTime.Now;
                model.LotteryType = 3;
                model.LotteryQS = data[0];
                model.openTime = DateTime.Parse(data[1]);
                model.LotteryNo = data[2] + "," + data[3] + "," + data[4];//开奖号码
                model.AppearOrderNo = "0";
                model.sumMoney = decimal.Parse(data[5]);
                model.jiangChi = 0;
                model.oneCount = int.Parse(data[6]);
                model.oneMoney = decimal.Parse(data[7]);
                model.twoCount = int.Parse(data[8]);
                model.twoMoney = decimal.Parse(data[9]);
                model.threeCount = int.Parse(data[10]);
                model.threeMoney = decimal.Parse(data[11]);
                model.fourCount = 0;
                model.fourMoney = 0;
                model.fiveCount = 0;
                model.fiveMoney = 0;
                model.sixCount = 0;
                model.sixMoney = 0;
                r = addWinInfo(model);
                #endregion
            }
            else if (type == 4)
            {
                #region 排五
                Model.LotteryAnnouncement model = new Model.LotteryAnnouncement();
                model.AddTime = System.DateTime.Now;
                model.LotteryType = 4;
                model.LotteryQS = data[0];
                model.openTime = DateTime.Parse(data[1]);
                model.LotteryNo = data[2] + "," + data[3] + "," + data[4] + "," + data[5];//开奖号码
                model.AppearOrderNo = "0";
                model.sumMoney = decimal.Parse(data[6]);
                model.jiangChi = 0;
                model.oneCount = int.Parse(data[7]);
                model.oneMoney = decimal.Parse(data[8]);
                model.twoCount = 0;
                model.twoMoney = 0;
                model.threeCount = 0;
                model.threeMoney = 0;
                model.fourCount = 0;
                model.fourMoney = 0;
                model.fiveCount = 0;
                model.fiveMoney = 0;
                model.sixCount = 0;
                model.sixMoney = 0;
                r = addWinInfo(model);
                #endregion
            }
            else if (type == 5)
            {
                #region 福彩3D
                Model.LotteryAnnouncement model = new Model.LotteryAnnouncement();
                model.AddTime = System.DateTime.Now;
                model.LotteryType = 5;
                model.LotteryQS = data[0];
                model.openTime = DateTime.Parse(data[1]);
                model.LotteryNo = data[2] + "," + data[3] + "," + data[4];//开奖号码
                model.AppearOrderNo = data[5] + "," + data[6] + "," + data[7] + "+" + data[8] + "," + data[9];//试机号+机球
                model.sumMoney = decimal.Parse(data[10]);
                model.jiangChi = 0;
                model.oneCount = int.Parse(data[11]);
                model.oneMoney = decimal.Parse(data[12]);
                model.twoCount = int.Parse(data[13]);
                model.twoMoney = decimal.Parse(data[14]);
                model.threeCount = int.Parse(data[15]);
                model.threeMoney = decimal.Parse(data[16]);
                model.fourCount = 0;
                model.fourMoney = 0;
                model.fiveCount = 0;
                model.fiveMoney = 0;
                model.sixCount = 0;
                model.sixMoney = 0;
                r = addWinInfo(model);
                #endregion
            }
            else if (type == 6)
            {
                #region 七星彩
                Model.LotteryAnnouncement model = new Model.LotteryAnnouncement();
                model.AddTime = System.DateTime.Now;
                model.LotteryType = 6;
                model.LotteryQS = data[0];
                model.openTime = DateTime.Parse(data[1]);
                model.LotteryNo = data[2] + "," + data[3] + "," + data[4] + "," + data[5] + "," + data[6] + "," + data[7] + "," + data[8];
                model.AppearOrderNo = "0";
                model.sumMoney = decimal.Parse(data[9]);
                model.jiangChi = decimal.Parse(data[10]);
                model.oneCount = int.Parse(data[11]);
                model.oneMoney = decimal.Parse(data[12]);
                model.twoCount = int.Parse(data[13]);
                model.twoMoney = decimal.Parse(data[14]);
                model.threeCount = int.Parse(data[15]);
                model.threeMoney = decimal.Parse(data[16]);
                model.fourCount = int.Parse(data[17]);
                model.fourMoney = decimal.Parse(data[18]);
                model.fiveCount = int.Parse(data[19]);
                model.fiveMoney = decimal.Parse(data[20]);
                model.sixCount = int.Parse(data[21]);
                model.sixMoney = decimal.Parse(data[22]);
                r = addWinInfo(model);
                #endregion
            }
            else if (type == 7)
            {
                #region 七乐彩
                Model.LotteryAnnouncement model = new Model.LotteryAnnouncement();
                model.AddTime = System.DateTime.Now;
                model.LotteryType = 7;
                model.LotteryQS = data[0];
                model.openTime = DateTime.Parse(data[1]);
                model.LotteryNo = data[2] + "," + data[3] + "," + data[4] + "," + data[5] + "," + data[6] + "," + data[7] + "," + data[8] + "+" + data[9];
                model.AppearOrderNo = "0";
                model.sumMoney = decimal.Parse(data[10]);
                model.jiangChi = decimal.Parse(data[11]);
                model.oneCount = int.Parse(data[12]);
                model.oneMoney = decimal.Parse(data[13]);
                model.twoCount = int.Parse(data[14]);
                model.twoMoney = decimal.Parse(data[15]);
                model.threeCount = int.Parse(data[16]);
                model.threeMoney = decimal.Parse(data[17]);
                model.fourCount = int.Parse(data[18]);
                model.fourMoney = decimal.Parse(data[19]);
                model.fiveCount = int.Parse(data[20]);
                model.fiveMoney = decimal.Parse(data[21]);
                model.sixCount = int.Parse(data[22]);
                model.sixMoney = decimal.Parse(data[23]);
                model.sevenCount = int.Parse(data[24]);
                model.sevenMoney = decimal.Parse(data[25]);
                r = addWinInfo(model);
                #endregion
            }
            return r;
        }

        private static long addWinInfo(Model.LotteryAnnouncement model)
        {
            long r = 0;
            string sql = "select top 1 * from LotteryAnnouncement where LotteryType=" + model.LotteryType + " and lotteryQs=" + model.LotteryQS;
            if (BLL.LotteryQs.NewsTopOne(sql) == null)
            {
                model.AddTime = DateTime.Now;
                r = BLL.LotteryAnnouncement.Add(model);
            }
            return r;

        }

        //=================2016.7.8=========================
        public static List<Model.LotteryAnnouncement> getDatas(int maxValue, int type)
        {
            List<Model.LotteryAnnouncement> arlist = new List<Model.LotteryAnnouncement>();
            if (maxValue > 2000)//按年份查询
            {
                int Count = 0;
                DateTime dt = DateTime.Now;
                string currentYear = dt.Year.ToString();
                if (currentYear == maxValue.ToString())//当前年份
                {
                    arlist = GetPageList_Where1(0, maxValue, type, out Count);

                }
                else//其他年份
                {
                    string sqlstr = "SELECT  * FROM LotteryAnnouncement WHERE LotteryType = " + type + " and DATEPART(YEAR, openTime)= '" + maxValue + "' ORDER BY id asc";
                    arlist = GetList(sqlstr);
                }
            }
            else//前N条
            {
                string sqlstr = "select * from (SELECT top(" + maxValue + ") * FROM LotteryAnnouncement WHERE (LotteryType = " + type + " and DATEPART(YEAR, openTime) = DATEPART(YEAR, GETDATE())) ORDER BY id DESC) a ORDER BY id asc";
                arlist = GetList(sqlstr);
            }
            return arlist;
        }
        /// <summary>
        /// 倒序排列
        /// </summary>
        /// <param name="maxValue"></param>
        /// <param name="type"></param>
        /// <returns></returns>
        public static List<Model.LotteryAnnouncement> getDatasDesc(int maxValue, int type)
        {
            List<Model.LotteryAnnouncement> arlist = new List<Model.LotteryAnnouncement>();
            if (maxValue > 2000)//按年份查询
            {
                int Count = 0;
                DateTime dt = DateTime.Now;
                string currentYear = dt.Year.ToString();
                if (currentYear == maxValue.ToString())//当前年份
                {
                    arlist = GetPageList_Where2(0, maxValue, type, out Count);

                }
                else//其他年份
                {
                    string sqlstr = "SELECT  * FROM LotteryAnnouncement WHERE LotteryType = " + type + " and DATEPART(YEAR, openTime)= '" + maxValue + "' ORDER BY id desc";
                    arlist = GetList(sqlstr);
                }
            }
            else//前N条
            {
                string sqlstr = "select * from (SELECT top(" + maxValue + ") * FROM LotteryAnnouncement WHERE (LotteryType = " + type + " and DATEPART(YEAR, openTime) = DATEPART(YEAR, GETDATE())) ORDER BY id desc) a ";
                arlist = GetList(sqlstr);
            }
            return arlist;
        }

        public static List<Model.LotteryAnnouncement> getDatas1(int maxValue, int type)
        {
            List<Model.LotteryAnnouncement> arlist = new List<Model.LotteryAnnouncement>();
            if (maxValue > 2000)//按年份查询
            {
                int Count = 0;
                DateTime dt = DateTime.Now;
                string currentYear = dt.Year.ToString();
                if (currentYear == maxValue.ToString())//当前年份
                {
                    arlist = GetPageList_Where2(0, maxValue, type, out Count);

                }
                else//其他年份
                {
                    string sqlstr = "SELECT  * FROM LotteryAnnouncement WHERE LotteryType =" + type + "  and DATEPART(YEAR, openTime)= '" + maxValue + "' ORDER BY id desc";
                    arlist = GetList(sqlstr);
                }
            }
            else//前N条
            {
                string sqlstr = "select * from (SELECT top(" + maxValue + ") * FROM LotteryAnnouncement WHERE (LotteryType = " + type + "  and DATEPART(YEAR, openTime) = DATEPART(YEAR, GETDATE())) ORDER BY id DESC) a ORDER BY id desc";
                arlist = GetList(sqlstr);
            }
            return arlist;
        }
    }
}